package com.gl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gl.entity.CrmBanner;
import com.gl.entity.EduCourse;
import com.gl.mapper.CrmBannerMapper;
import com.gl.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gl.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {
    @Autowired
    private EduCourseService courseService;

    //最新课程轮播生成
    @Override
    public void createBanner() {
        //删除所有banner
        baseMapper.delete(null);

        //查询课程表中最新三条课程并添加到banner表
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        wrapper.last("limit 3");
        List<EduCourse> list = courseService.list(wrapper);
        //将数据添加到banner表
        for (EduCourse eduCourse : list) {
            CrmBanner banner = new CrmBanner();
            banner.setTitle(eduCourse.getTitle());
            banner.setImageUrl(eduCourse.getCover());
            banner.setLinkUrl("http://localhost:8080/#/courseDetail/"+eduCourse.getId());
            baseMapper.insert(banner);
        }

    }
}
